<?php

/*
 * 导出exl
 */
function export($data = array(), $cell = array()) {
    $eu = new \Home\Util\ExcelUtil();
    $eu->exportExcel($data, $cell);
}

/*
 * 模糊查询参数生成
 * 字段由参数提供
 * mao
 */
function LIKEGET($data) {
    $res = array();
    foreach ($data as $k => $v) {
        $data = explode(',', $v);
        if (count($data) > 1) {
            $temp = array();
            foreach ($data as $k1 => $v1) {
                $temp[] = array('like',
                    '%' . $v1 . '%');
            }
            $temp[] = 'ThinkPHP';
            $temp[] = 'or';
            $res[$k] = $temp;
        } else {
            $res[$k] = array('like',
                '%' . $v . '%');
        }
    }
    return $res;
}


/*
 * 模糊查询参数生成
 * 字段由参数提供
 * mao
 */
function LIKEGET_ADMIN($data) {
    $res = array();
    foreach ($data as $k => $v) {
        $data = explode(',', $v);
        if (count($data) > 1) {
            $temp = array();
            foreach ($data as $k1 => $v1) {
                $temp[] = array('like',
                    '%' . $v1 . '%');
            }
            $temp[] = 'ThinkPHP';
            $temp[] = 'or';
            $res[$k] = $temp;
        } else {
            if ($v) {
                $res[$k] = array('like',
                    '%' . $v . '%');
            }
        }
    }
    return $res;
}

/*
 * 书籍模糊查询参数生成
 * 字段也模糊
 * mao
 */
function LIKEGET_MH($str) {

    preg_match_all('/[\x{4e00}-\x{9fa5}]/u', $str, $chinese);
    preg_match_all('/[^\x{4e00}-\x{9fa5}]/u', $str, $string);
    $result = array_merge(current($chinese), current($string));
    $MHSTR = '%';
    foreach ($result as $k => $v) {
        $MHSTR .= $v . '%';
    }
    $res = array();
    $res['TITLE'] = array('like',
        $MHSTR);
    $res['ISBN'] = array('like',
        '%' . $str . '%');
    $res['AUTHOR'] = array('like',
        $MHSTR);
    $res['PRESS'] = array('like',
        $MHSTR);
    $res['region_one'] = array('like',
        '%' . $str . '%');
    $res['region_two'] = array('like',
        '%' . $str . '%');
    $res['KEY'] = array('like',
        '%' . $str . '%');
    $res['_logic'] = 'OR';
    return $res;
}
/*
 * 书籍模糊查询参数生成字符串
 * 字段也模糊
 * mao
 */
function LIKEGET_MH_STR($str) {

    $res_str =  "(`TITLE` like '%".$str."%'";
    $res_str .="or `ISBN` like '%".$str."%'";
    $res_str .="or `AUTHOR` like '%".$str."%'";
    $res_str .="or `PRESS` like '%".$str."%'";
    $res_str .="or `region_one` like '%".$str."%'";
    $res_str .="or `region_two` like '%".$str."%'";
    $res_str .="or `KEY` like '%".$str."%')";
    return $res_str;
}
/*
 * 逐个汉字遍历字符串
 */
function str_split_unicode($str, $l = 0) {
    if ($l > 0) {
        $ret = array();
        $len = mb_strlen($str, "UTF-8");
        for ($i = 0; $i < $len; $i += $l) {
            $ret[] = mb_substr($str, $i, $l, "UTF-8");
        }
        return $ret;
    }
    return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY);
}

/*
 * 分页参数
 * 参数page=1
 * 参数page_num =10
 * 返回分页参数数组
 */
function GET_LIMIT($page, $page_num) {
    $limit = array();
    if ($page && $page_num) {
        $limit['start'] = ($page - 1) * $page_num;
        $limit['end'] = $page_num;
    }
    return $limit;
}

/*
 * 成色价格计算
 * 参数书籍原价  $Y_PRICE
 * 参数包装 BZ
 * 参数成色 CS
 * 返回 价格
 */
function GET_BOOK_PRICE($Y_PRICE, $BZ, $CS) {
    $BOOK_MONEY = C('BOOK_MONEY');
    $cost = $Y_PRICE;
    if ($BOOK_MONEY[$BZ] && $BOOK_MONEY[$BZ][$CS]) {
        $cost = $Y_PRICE * $BOOK_MONEY[$BZ][$CS];
    }
    return $cost;
}

/*
 * 时间日期选择器
 */
function SelectTime($files, $time1, $time2) {
    return $where[$files] = array('between',
        array($time1,
            $time2));
}


/*
 * 返回错误的信息
 * 参数 1 2 3 4 5
 * 返回不同错误值
 */
function ResErrorData($top = -1) {
    $str = "数据错误";
    switch ($top) {
        case 1:
            $str = '书籍仓库不足';
            break;
        case 2:
            $str = '数据修改失败';
            break;
        case 3:
            $str = '无对应书籍信息/书籍ID有误';
            break;
        case 4:
            $str = '亲！！已经没有其他书籍了哦';
            break;
        case 5:
            $str = '支付订单ID错误';
            break;
        case 6:
            $str = '订单状态修改，库存操作错误';
            break;
        case 7:
            $str = '添加扣除余额记录失败';
            break;
        case 8:
            $str = '修改余额失败';
            break;
        case 9:
            $str = '拼团已结束，请重新开团';
            break;
        case 10:
            $str = '提现金额有误';
            break;
        case 11:
            $str = '红包提现失败';
            break;
        case 12 :
            $str = '零钱提现失败';
            break;

    }
    $res['msg'] = -1;
    $res['desc'] = $str;
    return $res;
}

/*
 * 数据返回格式
 */
function ResData($DATA) {
    if ($DATA) {
        $res['datas'] = $DATA;
        $res['msg'] = 200;
        $res['desc'] = 'success';
        $res['res_num'] = count($DATA);
    } else {
        $res['msg'] = -1;
        $res['desc'] = 'error';
    }
    return $res;
}


/*
*功能：对字符串进行加密处理
*参数一：需要加密的内容
*参数二：密钥
*/
function passport_encrypt($str, $key) { //加密函数
    srand((double)microtime() * 1000000);
    $encrypt_key = md5(rand(0, 32000));
    $ctr = 0;
    $tmp = '';
    for ($i = 0; $i < strlen($str); $i++) {
        $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
        $tmp .= $encrypt_key[$ctr] . ($str[$i] ^ $encrypt_key[$ctr++]);
    }
    return base64_encode(passport_key($tmp, $key));
}

/*
*功能：对字符串进行解密处理
*参数一：需要解密的密文
*参数二：密钥
*/
function passport_decrypt($str, $key) { //解密函数
    $str = passport_key(base64_decode($str), $key);
    $tmp = '';
    for ($i = 0; $i < strlen($str); $i++) {
        $md5 = $str[$i];
        $tmp .= $str[++$i] ^ $md5;
    }
    return $tmp;
}

/*
*加密辅助函数
*/
function passport_key($str, $encrypt_key) {
    $encrypt_key = md5($encrypt_key);
    $ctr = 0;
    $tmp = '';
    for ($i = 0; $i < strlen($str); $i++) {
        $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
        $tmp .= $str[$i] ^ $encrypt_key[$ctr++];

    }
    return $tmp;
}


/*
 * 二维数组排序
 * $arrays 要排序的数组
 * $sort_key 要排序的字段key
 * $sort_order  SORT_ASC - 默认，按升序排列。(A-Z)SORT_DESC - 按降序排列。(Z-A)
 * $sort_type   SORT_REGULAR - 默认。将每一项按常规顺序排列。SORT_NUMERIC - 将每一项按数字顺序排列。SORT_STRING - 将每一项按字母顺序排列
 */
function my_sort($arrays, $sort_key, $sort_order = SORT_ASC, $sort_type = SORT_NUMERIC) {
    if (is_array($arrays)) {
        foreach ($arrays as $array) {
            if (is_array($array)) {
                $key_arrays[] = $array[$sort_key];
            } else {
                return false;
            }
        }
    } else {
        return false;
    }
    array_multisort($key_arrays, $sort_order, $sort_type, $arrays);
    return $arrays;
}
function http_request_Common($url, $data = NULL) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)) {
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}

function GetBookToIsbnInDDAPI($ISBN) {
    $url = 'http://www.qimiaoyue.com/appBook/ddBookDetail.do';
    $data['isbn'] = $ISBN;
    $res = http_request_Common($url, $data);
    $resData = json_decode($res, true);
    if ($resData['SUCCESS_CODE'] == 'true') {
        $resData['KEY'] = substr($resData['KEY'], strpos($resData['KEY'], '童书>'));
        $resData['Y_PRICE'] = trim($resData['Y_PRICE'], '¥');
        unset($resData['SUCCESS_CODE']);
        return $resData;
    } else {
        return -2;
    }
}


/*
 * 获取链接返回数据
 */
function getHTTPS($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

?>